home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C17 / CmpIter.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1001 b   |  36 lines

  1. //: C17:CmpIter.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Find a group of characters in a string
  7. #include <string>
  8. #include <iostream>
  9. using namespace std;
  10.  
  11. // Case insensitive compare function:
  12. int 
  13. stringCmpi(const string& s1, const string& s2) {
  14.   // Select the first element of each string:
  15.   string::const_iterator 
  16.     p1 = s1.begin(), p2 = s2.begin();
  17.   // Don't run past the end:
  18.   while(p1 != s1.end() && p2 != s2.end()) {
  19.     // Compare upper-cased chars:
  20.     if(toupper(*p1) != toupper(*p2))
  21.       // Report which was lexically  greater:
  22.       return (toupper(*p1)<toupper(*p2))? -1 : 1;
  23.     p1++;
  24.     p2++;
  25.   }
  26.   // If they match up to the detected eos, say 
  27.   // which was longer. Return 0 if the same.
  28.   return(s2.size() - s1.size());
  29. }
  30.  
  31. int main() {
  32.   string s1("Mozart");
  33.   string s2("Modigliani");
  34.   cout << stringCmpi(s1, s2) << endl;
  35. } ///:~
  36.